//
//  DataBaseManager.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "DataBaseManager.h"

static sqlite3 *dbPoint = nil;

@implementation DataBaseManager

//打开
+(void)openDataBase
{
    if(dbPoint)
    {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Special.sqlite"];
    sqlite3_open(path.UTF8String, &dbPoint);
//    NSLog(@"%@", path);
}
//关闭
+(void)closeDataBase
{
    int result = sqlite3_close(dbPoint);
    if (result == SQLITE_OK) {
//        NSLog(@"关闭成功");
        dbPoint = nil;
    }
    else
    {
//        NSLog(@"关闭失败:%d", result);
    }
}
//创建
+(void)createTable
{
    [self openDataBase];
    NSString *sqlString = @"CREATE TABLE IF NOT EXISTS SpecialList(RecipeId varchar(50) PRIMARY KEY, Title varchar(50))";
    int result = sqlite3_exec(dbPoint, sqlString.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"表格创建成功");
    }
    else
    {
//        NSLog(@"表格创建失败:%d", result);
    }
    [self closeDataBase];
}
//插入
+(void)insertSpecial:(CYNextSpecialModel *)Special
{
    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"INSERT INTO SpecialList VALUES('%@', '%@')", Special.RecipeId, Special.Title];
    int result = sqlite3_exec(dbPoint, sqlString.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }
    else
    {
//        NSLog(@"插入失败");
    }
    [self closeDataBase];
}
//删除
+(void)deleteSpecialByTitle:(NSString *)title;
{
    [self openDataBase];
    NSString *sqlString = [NSString stringWithFormat:@"DELETE FROM SpecialList WHERE Title = '%@'", title];
    int result = sqlite3_exec(dbPoint, sqlString.UTF8String, nil, nil, nil);
    if (result == SQLITE_OK) {
//        NSLog(@"删除成功");
    }
    else
    {
//        NSLog(@"删除失败"); 
    }
    [self closeDataBase];
}
//查询
+(NSMutableArray *)selectAllSpecial
{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sqlString = @"SELECT * FROM SpecialList";
    NSMutableArray *array = [NSMutableArray array];
    int result = sqlite3_prepare_v2(dbPoint, sqlString.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            const unsigned char *RecipeId = sqlite3_column_text(stmt, 0);
            const unsigned char *Title = sqlite3_column_text(stmt, 1);
            NSString *IDString = [NSString stringWithUTF8String:(const char *)RecipeId];
            NSString *titleString = [NSString stringWithUTF8String:(const char *)Title];
            CYNextSpecialModel *Special = [[CYNextSpecialModel alloc]init];
            Special.RecipeId = IDString;
            Special.Title = titleString;
            [array addObject:Special];
        }
    }
    else
    {
        
    }
    sqlite3_finalize(stmt);
    [self closeDataBase];
    
    return array;
}
//模糊
+(NSMutableArray *)selectAllSpecialByTitle:(NSString *)title
{
    return nil;
}

@end
